package com.vlong.town.controller.admin.labour;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vlong.town.controller.base.BaseController;
import com.vlong.town.entity.base.DataMessage;
import com.vlong.town.entity.hotel.HotelInfo;
import com.vlong.town.entity.labour.Labour;
import com.vlong.town.service.labour.LabourService;
import com.vlong.town.util.common.TownConstant;
import com.vlong.town.util.data.DataUtil;
import com.vlong.town.util.pagination.PageTag;

/**
 * 劳务管理
 * @author 51088
 */
@Controller
@RequestMapping("/labour")
public class LabourController extends BaseController{
	
	@Autowired
	private LabourService  labourService;
	/**
	 * 列表查询
	 * @param pageNo
	 * @param pageSize
	 * @param labourName
	 * @return
	 */
	@RequestMapping("/list")
	public  String  list(
			@RequestParam(required=false, value="pageNo") Integer pageNo,
            @RequestParam(required=false, value="pageSize") Integer pageSize,
            @RequestParam(required=false, value="labourName") String labourName
			){
		PageHelper.startPage(getPageNo(pageNo), getPageSize(pageSize));
        PageHelper.orderBy("update_time DESC");
        List<Labour> queryList = labourService.queryList(null, 0, TownConstant.TOWN_STATUS_2, labourName, null, null, null, null, null, null, null, null, null, null);
        PageInfo<Labour> pageInfo = new PageInfo<Labour>(queryList);
        PageTag<Labour> pt = new PageTag<Labour>(request, "list", pageInfo);
        request.setAttribute("pageHtml", pt.getPageHtml());
        request.setAttribute("labourName", labourName);
        request.setAttribute("status", 2);
        for(Labour labour : queryList) {
        	labour.setLabourDesc(DataUtil.delHTMLTag(labour.getLabourDesc(), 0));
        }
        request.setAttribute("labourList", queryList);
        return  "admin/labour/list_labour";
	}
	
	/**
	 * 列表查询
	 * @param pageNo
	 * @param pageSize
	 * @param labourName
	 * @return
	 */
	@RequestMapping("/listNo")
	public  String  listNo(
			@RequestParam(required=false, value="pageNo") Integer pageNo,
            @RequestParam(required=false, value="pageSize") Integer pageSize,
            @RequestParam(required=false, value="labourName") String labourName
			){
		PageHelper.startPage(getPageNo(pageNo), getPageSize(pageSize));
        PageHelper.orderBy("update_time DESC");
        List<Labour>  queryList = new ArrayList<Labour>();
        List<Labour> queryListOne = labourService.queryList(null, 0, TownConstant.TOWN_STATUS_1, labourName, null, null, null, null, null, null, null, null, null, null);
        List<Labour> queryListTwo = labourService.queryList(null, 0, TownConstant.TOWN_STATUS_3, labourName, null, null, null, null, null, null, null, null, null, null);
        queryList.addAll(queryListOne);
        queryList.addAll(queryListTwo);
        PageInfo<Labour> pageInfo = new PageInfo<Labour>(queryList);
        PageTag<Labour> pt = new PageTag<Labour>(request, "list", pageInfo);
        request.setAttribute("pageHtml", pt.getPageHtml());
        request.setAttribute("labourName", labourName);
        request.setAttribute("status", 1);
        for(Labour labour : queryList) {
        	labour.setLabourDesc(DataUtil.delHTMLTag(labour.getLabourDesc(), 0));
        }
        request.setAttribute("labourList", queryList);
        return  "admin/labour/list_labour";
	}
	
	/**
	 * 跳转添加页面
	 * @return
	 */
	@RequestMapping("/toAdd")
	public String toAdd(){
		return "admin/labour/add_labour";
	}
	
	/**
	 * 添加工作劳务
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/executeAddLabour")
	public DataMessage executeAddLabour(
			@RequestParam(required=false, value="labourName") String labourName,
			@RequestParam(required=false, value="companyName") String companyName,
			@RequestParam(required=false, value="labourDesc") String labourDesc,
			@RequestParam(required=false, value="labourEducation") String labourEducation,
			@RequestParam(required=false, value="workAddress") String workAddress,
			@RequestParam(required=false, value="labourMinYear") String labourMinYear,
			@RequestParam(required=false, value="labourMaxYear") String labourMaxYear,
			@RequestParam(required=false, value="labourMinPrice") String labourMinPrice,
			@RequestParam(required=false, value="labourMaxPrice") String labourMaxPrice
			){
		DataMessage dm=new  DataMessage();
		if(!validateStringProperty(labourName, 2, 20)){
			dm.setResult(TownConstant.RESULT_CODE_DATA_ERROR);
			dm.setMessage("劳务名称不能为空");
		}else{
			this.labourService.add(null, 0, 2, labourName, companyName, labourDesc, workAddress, Integer.parseInt(labourEducation), Integer.parseInt(labourMinYear), Integer.parseInt(labourMaxYear), Long.parseLong(labourMinPrice), Long.parseLong(labourMaxPrice), System.currentTimeMillis(), null);
			dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
			dm.setMessage("添加成功");
		}
		return  dm;
	}
	
	@RequestMapping("/toEdit/{labourId}")
	public  String toEdit(@PathVariable(required=false, value="labourId") String labourId){
		Labour labour = this.labourService.queryById(Long.parseLong(labourId));
		request.setAttribute("labour", labour);
		return  "admin/labour/edit_labour";
	}
	
	
	/**
	 * 修改工作劳务
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/executeEditLabour")
	public DataMessage executeEditLabour(
			@RequestParam(required=false, value="labourId") String labourId,
			@RequestParam(required=false, value="labourName") String labourName,
			@RequestParam(required=false, value="companyName") String companyName,
			@RequestParam(required=false, value="labourDesc") String labourDesc,
			@RequestParam(required=false, value="labourEducation") String labourEducation,
			@RequestParam(required=false, value="workAddress") String workAddress,
			@RequestParam(required=false, value="labourMinYear") String labourMinYear,
			@RequestParam(required=false, value="labourMaxYear") String labourMaxYear,
			@RequestParam(required=false, value="labourMinPrice") String labourMinPrice,
			@RequestParam(required=false, value="labourMaxPrice") String labourMaxPrice
			){
		DataMessage dm=new  DataMessage();
		if(!validateStringProperty(labourName, 2, 20)){
			dm.setResult(TownConstant.RESULT_CODE_DATA_ERROR);
			dm.setMessage("劳务名称不能为空");
		}else{
			this.labourService.edit(Long.parseLong(labourId), 0, 0, labourName, companyName, labourDesc, workAddress, Integer.parseInt(labourEducation), Integer.parseInt(labourMinYear), Integer.parseInt(labourMaxYear), Long.parseLong(labourMinPrice), Long.parseLong(labourMaxPrice), null, System.currentTimeMillis(),null);
			dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
			dm.setMessage("修改成功");
		}
		return  dm;
	}
	
	@ResponseBody
	@RequestMapping("/delete")
	public  DataMessage delete(
			@RequestParam(required=false, value="labourId") Long labourId
			){
		DataMessage dm=new  DataMessage();
		if(labourId != null && labourId > 0L) {
			this.labourService.edit(labourId, 1, null, null, null, null, null, null, null, null, null, null, null, null,null);
            dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("删除成功");
        } else {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("参数错误");
        }
		return dm;
	}
	
	@ResponseBody
	@RequestMapping("/batchDelete")
	public  DataMessage batchDelete(
			@RequestParam(required=false, value="labourIds") String labourIds
			){
		DataMessage dm=new  DataMessage();
		if(labourIds != null && labourIds.length()>0) {
			String[] split = labourIds.split(",");
			for (String labourId : split) {
				this.labourService.edit(Long.parseLong(labourId), 1, null, null, null, null, null, null, null, null, null, null, null, null,null);
			}
            dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("删除成功");
        } else {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("参数错误");
        }
		return dm;
	}
	
	/**
	 * 通过
	 * @param labourId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/adoptLabour")
	public  DataMessage adoptLabour(
			@RequestParam(required=false, value="labourId") Long labourId
			){
		DataMessage dm=new  DataMessage();
		if(labourId != null && labourId > 0L) {
			this.labourService.edit(labourId, null, TownConstant.TOWN_STATUS_2, null, null, null, null, null, null, null, null, null, null, null,null);
            dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("通过成功");
        } else {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("参数错误");
        }
		return dm;
	}
	/**
	 * 驳回
	 * @param labourId
	 * @return
	 */
	@RequestMapping("/rejectLabour")
	public  String rejectLabour(
			@RequestParam(required=false, value="labourId") Long labourId
			){
		request.setAttribute("labourId", labourId);
		return "admin/labour/reject_labour";
	}
	
	
	
	 /**
     * 更新驳回原因
     * @param hotelId 主键值
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("/updateReason")
    public DataMessage updateReason(
    		@RequestParam(required=true, value="labourId") Long labourId,
    		@RequestParam(required=true, value="reason") String reason
    		) {
        DataMessage dm = new DataMessage();
        if(labourId != null && labourId > 0L) {
        	this.labourService.edit(labourId, null, TownConstant.TOWN_STATUS_3, null, null, null, null, null, null, null, null, null, null, null,reason);
            dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("驳回成功");
        } else {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("参数错误");
        }
        return dm;
    }
    
    
    /**
	 * 查看驳回原因
	 * @return
	 */
	@RequestMapping(value="/viewLabour")
	public  String  viewLabour(@RequestParam(required=true, value="labourId") Long labourId){
		Labour labour = labourService.queryById(labourId);
		request.setAttribute("labour", labour);
		return "admin/labour/view_labour";
	}
    
    
}
